// DriveNotes. Copyright (c) 2011, Andrei Senchuk. All rights reserved.
package net.taviscaron.drivenotes.server.model.validators;

import net.taviscaron.drivenotes.shared.exceptions.InvalidEntityException;
import net.taviscaron.drivenotes.shared.model.entity.Note;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static net.taviscaron.drivenotes.server.util.ExceptionUtils.invalidEntityException;

/**
 * Note validator
 * @author Andrei Senchuk
 */
public class NoteValidator implements Validator<Note> {
    private static final int NAME_MAX_SIZE = 255;
    private static final Log logger = LogFactory.getLog(NoteValidator.class);

    public enum Error {
        NOTE_NAME_IS_EMPTY, NAME_IS_TOO_LONG
    }

    public void validate(Note note) throws InvalidEntityException {
        if(note.getAuthor() == null) {
            logger.warn("Trying to save note with null author");
            throw invalidEntityException(null);
        }

        if(note.getName() == null || note.getName().trim().isEmpty()) {
            throw invalidEntityException(Error.NOTE_NAME_IS_EMPTY);
        }

        if(note.getName().length() > NAME_MAX_SIZE) {
            throw invalidEntityException(Error.NAME_IS_TOO_LONG);
        }
    }
}
